ASP創建SQL Server數據庫的兩種方法
作者:tank 日期:2004-12-29
ASP創建SQL Server數據庫的兩種方法
<%
************ 使用ADODB.Connect對象創建 ************************
Dim oConn
Dim sDatabaseName
sDatabaseName = "CodeCreateDB"
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "Provider=SQLOLEDB;Data Source=(local);User ID=sa;Password=;"
oConn.Execute "Create DATABASE " & sDatabaseName
%>
方法二:
<%@ Language=VBScript %>
<%
******************* 使用 SQLDMO Object 對象庫 *******************
Dim oSQLServer
Dim oDatabase
Dim oDBFileData
Dim oLogFile
Dim sDatabaseName
Dim sDatapath
數據庫名字
sDatabaseName = "CodeCreateDBTest"
數據文件保存路徑
sDatapath = "D:\"
創建Sql Server對象并進行鏈接,(local)處為Server名字
Set oSQLServer = Server.CreateObject("SQLDMO.SQLServer")
oSQLServer.LoginSecure = True 使用集成驗證integrated security
oSQLServer.Connect "(local)"
oSQLServer.Connect "(local)", "sa", "" 使用標準驗證 standard security
創建Database對象
Set oDatabase = Server.CreateObject("SQLDMO.Database")
oDatabase.Name = sDatabaseName
創建db文件對象
Set oDBFileData = Server.CreateObject("SQLDMO.DBFile")
With oDBFileData
.Name = sDatabaseName & "_data"
.PhysicalName = sDatapath & "\" & sDatabaseName & "_data.mdf"
.PrimaryFile = True
.FileGrowthType = SQLDMOGrowth_MB
.FileGrowth = 1
End With
創建日志文件對象
Set oLogFile = Server.CreateObject("SQLDMO.LogFile")
With oLogFile
.Name = sDatabaseName & "_log"
.PhysicalName = sDatapath & "\" & sDatabaseName & "_log.ldf"
End With
把DB文件對象和日志文件對象添加到DataBase數據庫對象
oDatabase.FileGroups("PRIMARY").DBFiles.Add oDBFileData
oDatabase.TransactionLog.LogFiles.Add oLogFile
把數據庫添加到Sql server(create the database)
oSQLServer.Databases.Add oDatabase
關閉連接
oSQLServer.Close
釋放對象
Set oLogFile = Nothing
Set oDBFileData = Nothing
Set oDatabase = Nothing
Set oSQLServer = Nothing
%>
![Loading Vote](/Plugins/ArticleVote/sendVote.gif)